package com.anyisn.anyflickr.shell.view
{
	import com.anyisn.anyflickr.common.ApplicationConstants;
	import com.anyisn.anyflickr.shell.ShellFacade;
	import com.anyisn.anyflickr.shell.model.ListPhotosProxy;
	import com.anyisn.anyflickr.shell.model.SettingsProxy;
	import com.anyisn.anyflickr.shell.model.SlidePhotosProxy;
	import com.anyisn.anyflickr.shell.model.vo.TranslateVO;
	import com.anyisn.anyflickr.shell.view.components.popup.SearchTW;
	import com.anyisn.puremvc.as3.multicore.patterns.mediator.AbstractPopupMediator;
	
	import flash.events.Event;
	
	import mx.collections.ArrayCollection;
	import mx.events.CloseEvent;
	
	import org.puremvc.as3.multicore.interfaces.INotification;

	public class SearchTWMediator extends AbstractPopupMediator
	{
		public static const NAME:String = "SearchTWMediator";
		public var noteName:String;
		
		public function SearchTWMediator(viewComponent:Object, noteName:String)
		{
			this.noteName = noteName;
			super(NAME, viewComponent);
			searchTW.addEventListener(CloseEvent.CLOSE, close);
			searchTW.addEventListener(SearchTW.SEARCH, search);
//			searchTW.addEventListener(SearchTW.TRANSLATE, translate);
//			searchTW.trans_from.dataProvider = new ArrayCollection(ApplicationConstants.LANGUAGE_FROM);
//			searchTW.trans_to.dataProvider = new ArrayCollection(ApplicationConstants.LANGUAGE_TO);
		}
		
		override public function listNotificationInterests():Array {
			return [
				ShellFacade.GOOGLE_TRANSLATE_RESULT
			];
		}
		
		override public function handleNotification(notification:INotification):void {
			switch(notification.getName()) {
				case ShellFacade.GOOGLE_TRANSLATE_RESULT:
					searchTW.txt.text = Object(notification.getBody()).responseData.translatedText;
					break;
			}
		}
		
		override public function onRegister() : void {
			switch(noteName) {
				case ShellFacade.TILE_PHOTOS_SEARCH:
					var photosP0:ListPhotosProxy = facade.retrieveProxy(ListPhotosProxy.NAME) as ListPhotosProxy;
					searchTW.searchVO = photosP0.searchVO;
					var proxy:SettingsProxy = facade.retrieveProxy(SettingsProxy.NAME) as SettingsProxy;
					searchTW.perpage.value = proxy.tilePerpage;
					break
				case ShellFacade.SLIDE_PHOTOS_SEARCH:
					var photosP1:SlidePhotosProxy = facade.retrieveProxy(SlidePhotosProxy.NAME) as SlidePhotosProxy;
					searchTW.searchVO = photosP1.searchVO;
					var proxy:SettingsProxy = facade.retrieveProxy(SettingsProxy.NAME) as SettingsProxy;
					searchTW.perpage.value = proxy.slidePerpage;
					break;
			}
		}
		
		private function close(e:CloseEvent = null):void {
			closePopUpWindow(searchTW, NAME);
		}
		
		private function search(e:Event):void {
			sendNotification(noteName, [searchTW.searchVO, searchTW.perpage.value]);
			close();
		}
		
		/*private function translate(e:Event):void {
			var transVO:TranslateVO = new TranslateVO(searchTW.trans_from.selectedItem.value,searchTW.trans_to.selectedItem.value, searchTW.txt.text);
			sendNotification(ShellFacade.GOOGLE_TRANSLATE_GET, transVO);
		}*/
		
		private function get searchTW():SearchTW {
			return viewComponent as SearchTW;
		}
	}
}